//
//  CZView.swift
//  CZWeiBo1
//
//  Created by 1 on 15/11/22.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit


//访客视图
class CZView: UIView {
//swiftl所有的view都是通过xib、storyBoard加载的
    
   required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
    
    
    
    }
    
    override init(frame: CGRect) {
          super.init(frame: frame)
        
        //设置背景的颜色
//        backgroundColor=UIColor(red: 237/250.0, green: 237/250.0, blue: 237/250.0, alpha: 1)
        backgroundColor=UIColor(white: 237/255.0, alpha: 1)
//       backgroundColor=UIColor.grayColor()
        
        
        
        prepareUI()
        
    }
  
    //设置UI界面的操作
   private func prepareUI(){
      //添加自控键
   
     addSubview(iconView)
    addSubview(homeView)
       addSubview(coverView)
    addSubview(messageLabel)
    addSubview(registerButton)
    addSubview(loginButton)
  
    //添加约束 设置约束的view又负空间的啥时候才能设置他的自动布局约束
    //首先关闭autosizeing约束添加到view上面如果不清楚据添加到负空间爱你的上面
    iconView.translatesAutoresizingMaskIntoConstraints=false
    homeView.translatesAutoresizingMaskIntoConstraints=false
    registerButton.translatesAutoresizingMaskIntoConstraints=false
    loginButton.translatesAutoresizingMaskIntoConstraints=false
    messageLabel.translatesAutoresizingMaskIntoConstraints=false
    coverView.translatesAutoresizingMaskIntoConstraints=false
//    NSLayoutConstraint()
//    addConstraint(NSLayoutConstraint)
     //水平局居中
      addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    //垂直居中
    addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -80))
   //添加小房子
    addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    
    
    

    
    
    
    //添加消息
    addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
   addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
    //设置label 宽
    addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 230))
    
    
    //注册的按钮
     addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
    //添加登录的按钮
    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: loginButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))

      //设置遮盖饿视图

    
    
    addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -0))
    addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
    
    
    }
    
    //创建一个方法用与外部来设置方可视图的样式
    func setupVistorView(imageName:String,message:String){
      iconView.image=UIImage(named: imageName)
      
        messageLabel.text=message
         messageLabel.sizeToFit()
        homeView.hidden=true
        coverView.hidden=true
        //把在空间移到后面
//        sendSubviewToBack(coverView)
        //移动到最前面
//        bringSubviewToFront(coverView)
    
    }
  //懒加载控件
    //加载转盘
    private lazy var iconView:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //小房子
    private lazy var homeView:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息
    private lazy var messageLabel:UILabel={
       let label=UILabel()
        label.textColor=UIColor.darkGrayColor()
        label.text="嘎哈是客户噶开家阿哥拉斯的价格拉萨的感觉了啦 垃圾股始广"
      label.numberOfLines=0
        //更具文本来适应大小
        label.sizeToFit()
//        self.bringSubviewToFront(label)
        return label
   
    }()
    //注册按钮
    private lazy var registerButton:UIButton={
      let button=UIButton()
       button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font=UIFont.systemFontOfSize(14)
//        self.bringSubviewToFront(button)
        button.sizeToFit()
        return button
    }()
    //注册按钮
    private lazy var loginButton:UIButton={
       var button=UIButton()
        //设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.titleLabel?.font=UIFont.systemFontOfSize(14)
//        self.bringSubviewToFront(button)
        button.sizeToFit()
        
        return button
    }()
    
    //创建一个遮盖 的视图
    private lazy var coverView:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
}
